//15.三数之和
//https://leetcode.cn/problems/3sum
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(),nums.end());
        vector<vector<int>> ret;
        for(int i = 0;i<n;++i)
        {
            int l = i+1,r = n-1;
            while(l < r)
            {
                int sum = nums[i]+nums[l]+nums[r];
                if(sum < 0) ++l;
                else if(sum > 0) --r;
                else
                {
                    ret.push_back({nums[i],nums[l],nums[r]});
                    ++l;
                    --r;
                    while(l<r && nums[l]==nums[l-1]) ++l;
                    while(l<r && nums[r]==nums[r+1]) --r;
                }
            }
            while(i+1<n && nums[i]==nums[i+1]) ++i;
        }
        return ret;
    }
};